package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelVideoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    public static int type;
    private boolean isSelectAll;
    private SelVideoAdapter videoAdapter;
    private int oldPosition = 0;
    private String selPath = "";
    private List<String> listPath = new ArrayList<>();
    private String again = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSelVideoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(getApplicationContext(), 3));
        videoAdapter = new SelVideoAdapter();
        videoAdapter.setEdit(true);
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
        mDataBinding.llSelectAll.setOnClickListener(this);
        //视频拼接再次进入选择
        again = getIntent().getStringExtra(Content.Again);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSelectAll://全选
                listPath.clear();
                if (isSelectAll) {
                    for (SelectMediaEntity validDatum : videoAdapter.getValidData()) {
                        validDatum.setChecked(false);
                    }
                } else {
                    for (SelectMediaEntity validDatum : videoAdapter.getValidData()) {
                        validDatum.setChecked(true);
                        listPath.add(validDatum.getPath());
                    }
                }
                isSelectAll();
                videoAdapter.notifyDataSetChanged();
                break;
            case R.id.tvConfirm:
                if (selPath.isEmpty() && listPath.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_video));
                    return;
                }
                if (type == 0) {
                    if (TextUtils.isEmpty(again)) {
                        VideoSpliceActivity.listPath = listPath;
                        startActivity(VideoSpliceActivity.class);
                    } else {
                        if (listPath.size() > 0) {
                            ToastUtils.showShort(getString(R.string.please_sel_one_video));
                            return;
                        }
                        //更换/添加视频
                        Intent intent = new Intent();
                        intent.putExtra(Content.videoPath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else {
                    if (listPath.size() > 0) {
                        ToastUtils.showShort(getString(R.string.please_sel_one_video));
                        return;
                    }
                    if (type == 1) {
                        VideoCompressActivity.videoPath = selPath;
                        startActivity(VideoCompressActivity.class);
                    } else if (type == 2) {
                        VideoFormatActivity.videoPath = selPath;
                        startActivity(VideoFormatActivity.class);
                    } else if (type == 3) {
                        VideoTextActivity.videoPath = selPath;
                        startActivity(VideoTextActivity.class);
                    } else if (type == 4) {
                        VideoSpeedActivity.videoPath = selPath;
                        startActivity(VideoSpeedActivity.class);
                    } else if (type == 5) {
                        AudioExtractActivity.videoPath = selPath;
                        startActivity(AudioExtractActivity.class);
                    }
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0) {
            if (TextUtils.isEmpty(again)) {
                if (videoAdapter.getItem(position).isChecked()) {
                    videoAdapter.getItem(position).setChecked(false);
                    listPath.remove(videoAdapter.getItem(position).getPath());
                } else {
                    videoAdapter.getItem(position).setChecked(true);
                    listPath.add(videoAdapter.getItem(position).getPath());
                }
                videoAdapter.notifyItemChanged(position);
                isSelectAll();
            } else {
                videoAdapter.getItem(oldPosition).setChecked(false);
                videoAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                videoAdapter.getItem(position).setChecked(true);
                videoAdapter.notifyItemChanged(position);
                selPath = videoAdapter.getItem(position).getPath();
                mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 1));
            }
        } else {
            videoAdapter.getItem(oldPosition).setChecked(false);
            videoAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            videoAdapter.getItem(position).setChecked(true);
            videoAdapter.notifyItemChanged(position);
            selPath = videoAdapter.getItem(position).getPath();
            mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 1));
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getPermission();
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvVideoList.setVisibility(View.GONE);
                        mDataBinding.ivSelVideoNoData.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.ivSelVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.ivSelVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void isSelectAll() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listPath.size()));
        if (listPath.size() == videoAdapter.getValidData().size()) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.ayiquanx);
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanx);
            isSelectAll = false;
        }
    }

}